﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace CPPEI.Coolzon.UI
{
    public class DoubleTextbox : System.Windows.Controls.TextBox
    {
        public DoubleTextbox()
        {
            DataObject.AddPastingHandler(this, textBox1_Pasting);
            VerticalAlignment = VerticalAlignment.Center;
        }
        private void textBox1_Pasting(object sender, DataObjectPastingEventArgs e)
        {
            if (e.DataObject.GetDataPresent(typeof(string)))
            {
                string text = (string)e.DataObject.GetData(typeof(string));
                string finalText = Text.Insert(SelectionStart, text);
                double tryDouble;
                if (!Double.TryParse(finalText, out tryDouble))
                { e.CancelCommand(); }
            }
            else { e.CancelCommand(); }
        }
        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);
            bool isShift = false;
            System.Windows.Input.KeyboardDevice kd = e.KeyboardDevice;
            if ((kd.GetKeyStates(Key.LeftShift) & System.Windows.Input.KeyStates.Down) > 0 ||
                (kd.GetKeyStates(Key.RightShift) & System.Windows.Input.KeyStates.Down) > 0)
            {
                isShift = true;
            }
            int PPos = this.Text.IndexOf('.');//获取当前小数点位置
            if (((e.Key >= Key.D1 && e.Key <= Key.D9)//数字0-9键
                || (e.Key >= Key.NumPad1 && e.Key <= Key.NumPad9)) && !isShift)//小键盘数字0-9
            {
                if (this.SelectionStart > 0)
                {
                    if (this.Text.Substring(0, 1) == "0" && PPos != 1)
                    {//首位数字不为0
                        this.Text = this.Text.Remove(0, 1);
                    }
                }
                e.Handled = false;
                return;
            }
            else if ((e.Key == Key.D0 || e.Key == Key.NumPad0) && !isShift)//输入的是0
            {
                if (this.SelectionStart > 0)
                {//非首位输入
                    if (this.Text.Substring(0, 1) != "0")
                    {//首位数字不为0
                        e.Handled = false;
                        return;
                    }
                    else
                    {//首位数字为0,
                        if (PPos == 1)
                        { //0后面是小数点,小数位数未满
                            e.Handled = false;
                            return;
                        }
                    }
                }
                else
                {
                    e.Handled = false;
                    return;
                }
            }
            else if ((e.Key == Key.Decimal || e.Key == Key.OemPeriod) && !isShift)//小数点
            {
                if (PPos == -1)//未输入过小数点,且允许输入小数
                {
                    if (this.SelectionStart == 0)
                    {
                        this.Text = "0";
                        this.Select(this.Text.Length, 0);
                    }
                    e.Handled = false;
                    return;
                }
            }
            else if (e.Key == Key.Back)
            {
                e.Handled = false;
                return;
            }
            else if ((e.Key == Key.OemMinus || e.Key == Key.Subtract) && !isShift)
            {
                if (this.SelectionStart == 0)
                {
                    //首位输入
                    if (string.IsNullOrWhiteSpace(this.Text) || this.Text.Substring(0, 1) != "-")
                    {//首位不为-
                        e.Handled = false;
                        return;
                    }
                }
            }
            e.Handled = true;
        }
    }
}
